﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace CMS_GYM
{
    public class clsLoaiKhachHangDAO
    {

        /// <summary>
        /// Kết nối SQL Server
        /// </summary>
        public CSQLServer sqlServer = new CSQLServer();

        /// <summary>
        /// Lấy DS Loai khách hàng
        /// </summary>
        public DataTable LayBang()
        {
            string sql = "spGetBangLoaiKhachHang";
            DataTable table = sqlServer.readData(sql);
            return table;
        }

        /// <summary>
        /// Lay thong tin khach hang theo Loai Khach Hang ID
        /// </summary>
        /// <param name="MaKhachHang"></param>
        /// <returns></returns>
        public clsLoaiKhachHangDTO LayThongTinLoaiKhachHang(int LoaiKhachHangId)
        {
            clsLoaiKhachHangDTO LoaiKhachHang = null;
            string sql = "spGetInfoLoaiKhachHang";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@LoaiKhachHangId";
            valueofParameter[0] = LoaiKhachHangId;
            DataTable table = sqlServer.readData(sql, ParameterColection, valueofParameter);
            if (table != null && table.Rows.Count > 0)
            {
                LoaiKhachHang = ChuyenDoi(table.Rows[0]);
            }
            return LoaiKhachHang;
        }


        private clsLoaiKhachHangDTO ChuyenDoi(DataRow row)
        {
            try
            {
                clsLoaiKhachHangDTO KhachHang = null;
                if (row != null)
                {
                    KhachHang = new clsLoaiKhachHangDTO();
                    KhachHang.LoaiKhachHangId = int.Parse(row["LoaiKhachHangId"].ToString());
                    KhachHang.MaLoaiKhachHang = row["MaLoaiKhachHang"] == null ? string.Empty : row["MaLoaiKhachHang"].ToString();
                    KhachHang.TenLoaiKhachHang = row["TenLoaiKhachHang"] == null ? string.Empty : row["TenLoaiKhachHang"].ToString();
                    KhachHang.MoTa = row["MoTa"] == null ? string.Empty : row["MoTa"].ToString();
                    KhachHang.SoThuTu = row["SoThuTu"] == null || string.IsNullOrEmpty(row["SoThuTu"].ToString()) ? 0 : int.Parse(row["SoThuTu"].ToString());
                    KhachHang.TrangThai = row["TrangThai"] == null ? false : bool.Parse(row["TrangThai"].ToString());
                    KhachHang.NguoiTaoId = row["NguoiTaoId"] == null || string.IsNullOrEmpty(row["NguoiTaoId"].ToString()) ? 0 : int.Parse(row["NguoiTaoId"].ToString());
                    KhachHang.NgayTao = DateTime.Parse(row["NgayTao"].ToString());
                    KhachHang.NguoiChinhSuaId = row["NguoiChinhSuaId"] == null || string.IsNullOrEmpty(row["NguoiChinhSuaId"].ToString()) ? null : (int?)int.Parse(row["NguoiChinhSuaId"].ToString());
                    KhachHang.NgayChinhSua = row["NgayChinhSua"] == null || string.IsNullOrEmpty(row["NgayChinhSua"].ToString()) ? null : (DateTime?)DateTime.Parse(row["NgayChinhSua"].ToString());
                }
                return KhachHang;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

       
        /// <summary>
        /// Lấy Mã Khách hàng phat sinh tiep theo
        /// </summary>
        public string LayMaLoaiKhachHangMoi()
        {
            string sql = "spGetNewMaLoaiKhachHang";
            return sqlServer.readData(sql, "@MaLoaiKhachHang");
        }

        /// <summary>
        /// Cập nhật khách hàng
        /// </summary>
        public int Sua(clsLoaiKhachHangDTO LoaiKhachHang)
        {
            int i = -1;
            string sql = "sp_UpdateLoaiKhachHang";
            string[] ParameterColection = new string[4];
            Object[] valueofParameter = new Object[4];
            ParameterColection[0] = "@LoaiKhachHangId";
            valueofParameter[0] = LoaiKhachHang.LoaiKhachHangId;
            ParameterColection[1] = "@MaLoaiKhachHang";
            valueofParameter[1] = LoaiKhachHang.MaLoaiKhachHang;
            ParameterColection[2] = "@TenLoaiKhachHang";
            valueofParameter[2] = LoaiKhachHang.TenLoaiKhachHang;
            ParameterColection[3] = "@MoTa";
            valueofParameter[3] = LoaiKhachHang.MoTa;

            //string sql = "spUpdateLoaiKhachHang";
            //string[] ParameterColection = new string[8];
            //Object[] valueofParameter = new Object[8];
            //ParameterColection[0] = "@LoaiKhachHangId";
            //valueofParameter[0] = LoaiKhachHang.LoaiKhachHangId;
            //ParameterColection[1] = "@MaLoaiKhachHang";
            //valueofParameter[1] = LoaiKhachHang.MaLoaiKhachHang;
            //ParameterColection[2] = "@TenLoaiKhachHang";
            //valueofParameter[2] = LoaiKhachHang.TenLoaiKhachHang;
            //ParameterColection[3] = "@MoTa";
            //valueofParameter[3] = LoaiKhachHang.MoTa;
            //ParameterColection[4] = "@SoThuTu";
            //valueofParameter[4] = LoaiKhachHang.SoThuTu;
            //ParameterColection[5] = "@NguoiChinhSuaId";
            //valueofParameter[5] = LoaiKhachHang.NguoiChinhSuaId;
            //ParameterColection[6] = "@NgayChinhSua";
            //valueofParameter[6] = LoaiKhachHang.NgayChinhSua;
            //ParameterColection[7] = "@TrangThai";
            //valueofParameter[7] = LoaiKhachHang.TrangThai;
            
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Thêm khách hàng
        /// </summary>
        public int Them(clsLoaiKhachHangDTO LoaiKhachHang)
        {
            int i = -1;
            string sql = "spInsertLoaiKhachHang";
            string[] ParameterColection = new string[5];
            Object[] valueofParameter = new Object[5];
            ParameterColection[0] = "@MaLoaiKhachHang";
            valueofParameter[0] = LoaiKhachHang.MaLoaiKhachHang;
            ParameterColection[1] = "@TenLoaiKhachHang";
            valueofParameter[1] = LoaiKhachHang.TenLoaiKhachHang;
            ParameterColection[2] = "@MoTa";
            valueofParameter[2] = LoaiKhachHang.MoTa;
            ParameterColection[3] = "@SoThuTu";
            valueofParameter[3] = LoaiKhachHang.SoThuTu;
            ParameterColection[4] = "@NguoiTaoId";
            valueofParameter[4] = LoaiKhachHang.NguoiTaoId;
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Xóa khách hàng
        /// </summary>
        public int Xoa(int LoaiKhachHangId)
        {
            int i = -1;
            string sql = "spDeleteLoaiKhachHang";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@LoaiKhachHangId";
            valueofParameter[0] = LoaiKhachHangId;
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

    }
}
